package com.book.controller;

import com.book.entity.BookInfo;
import com.book.service.BookInfoService;
import com.book.util.ResultBack;
import com.github.pagehelper.PageInfo;
import org.aopalliance.intercept.Interceptor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Controller
@RequestMapping("/books")
public class BookInfoController {
    @Resource
    private BookInfoService bookInfoService;
    
    @RequestMapping("/delBookInfo")
    @ResponseBody
    public ResultBack delBookInfo(BookInfo bookInfo) throws Exception {
        int i = bookInfoService.changeBookInfoIsDeleted(bookInfo);
        if (i != 0) {
            return ResultBack.success("删除成功", i);
        } else {
            return ResultBack.failure("删除失败");
        }
    }

    @RequestMapping("/updateBookInfo")
    @ResponseBody
    public ResultBack updateBookInfo(BookInfo bookInfo) throws Exception {
        int i = bookInfoService.updateBookInfo(bookInfo);
        if (i != 0) {
            return ResultBack.success("更新成功", i);
        } else {
            return ResultBack.failure("更新失败");
        }
    }

    @RequestMapping("/selectBookByName")
    @ResponseBody
    public ResultBack selectBookByName(String bookName) throws Exception {
        List<BookInfo> bookInfoList = bookInfoService.selectBookByName(bookName);
        if (bookInfoList.isEmpty()) {
            return ResultBack.failure("查找失败");
        } else {
            return ResultBack.success("查找成功", bookInfoList);
        }
    }

    @RequestMapping("/selectBookInfoByPage")
    @ResponseBody
    public ResultBack selectBookInfoByPage(Integer pageNum, Integer pageSize) throws Exception {
        PageInfo<BookInfo> bookInfoList = bookInfoService.selectBookInfoByPage(pageNum, pageSize);
        return ResultBack.success("分页成功", bookInfoList);
    }


    @RequestMapping("/selectBookInfoConditions")
    @ResponseBody
    public ResultBack selectBookInfoConditions(BookInfo bookInfo, Date endPublisherDate, Double endPrice) throws Exception {
        List<BookInfo> bookInfoList = bookInfoService.selectBookInfoConditions(bookInfo, endPublisherDate, endPrice);
        if (bookInfoList.isEmpty()) {
            return ResultBack.failure("查找失败");
        } else {
            return ResultBack.success("查找成功", bookInfoList);
        }

    }

    @RequestMapping(path={"/selectBookInfoBy"})
    public String selectBookInfoByBookSaleCount(Model model) throws Exception {
        List<BookInfo> bookInfoListSale = bookInfoService.selectBookInfoByBookSaleCount();
        List<BookInfo> bookInfoListPublish = bookInfoService.selectBookInfoByBookByPublisherDate();
        List<BookInfo> bookInfoListCollection = bookInfoService.selectBookInfoByBookCollectionCount();
        model.addAttribute("bookInfoListSale", bookInfoListSale);
        model.addAttribute("bookInfoListPublish",bookInfoListPublish);
        model.addAttribute("bookInfoListCollection",bookInfoListCollection);
        return "index";
    }

    @RequestMapping("/selectBookInfoById")
    public String selectBookInfoById(Integer id,Model model) throws Exception {
        BookInfo bookInfoById = bookInfoService.selectBookInfoById(id);
        model.addAttribute("bookInfoById",bookInfoById);
        return "book-detail";
    }


}
